/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package moodlerest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

/**
 *
 * @author Lauro
 */
public class Util {

    public static String convertStreamToString(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
            if (line.contains("<?xml")) {
                sb.append(getEntitiesXML());
            }
        }
        is.close();
        return sb.toString();
    }

    private static String getEntitiesXML() {
        return "<!DOCTYPE moodle_xml [ "
                + "<!ENTITY nbsp   \"&#160;\" >\n"
                + "<!ENTITY iexcl  \"&#161;\" >\n"
                + "<!ENTITY cent   \"&#162;\" >\n"
                + "<!ENTITY pound  \"&#163;\" >\n"
                + "<!ENTITY curren \"&#164;\" >\n"
                + "<!ENTITY yen    \"&#165;\" >\n"
                + "<!ENTITY brvbar \"&#166;\" >\n"
                + "<!ENTITY sect   \"&#167;\" >\n"
                + "<!ENTITY uml    \"&#168;\" >\n"
                + "<!ENTITY copy   \"&#169;\" >\n"
                + "<!ENTITY ordf   \"&#170;\" >\n"
                + "<!ENTITY laquo  \"&#171;\" >\n"
                + "<!ENTITY not    \"&#172;\" >\n"
                + "<!ENTITY shy    \"&#173;\" >\n"
                + "<!ENTITY reg    \"&#174;\" >\n"
                + "<!ENTITY macr   \"&#175;\" >\n"
                + "<!ENTITY deg    \"&#176;\" >\n"
                + "<!ENTITY plusmn \"&#177;\" >\n"
                + "<!ENTITY sup2   \"&#178;\" >\n"
                + "<!ENTITY sup3   \"&#179;\" >\n"
                + "<!ENTITY acute  \"&#180;\" >\n"
                + "<!ENTITY micro  \"&#181;\" >\n"
                + "<!ENTITY para   \"&#182;\" >\n"
                + "<!ENTITY middot \"&#183;\" >\n"
                + "<!ENTITY cedil  \"&#184;\" >\n"
                + "<!ENTITY sup1   \"&#185;\" >\n"
                + "<!ENTITY ordm   \"&#186;\" >\n"
                + "<!ENTITY raquo  \"&#187;\" >\n"
                + "<!ENTITY frac14 \"&#188;\" >\n"
                + "<!ENTITY frac12 \"&#189;\" >\n"
                + "<!ENTITY frac34 \"&#190;\" >\n"
                + "<!ENTITY iquest \"&#191;\" >\n"
                + "<!ENTITY Agrave \"&#192;\" >\n"
                + "<!ENTITY Aacute \"&#193;\" >\n"
                + "<!ENTITY Acirc  \"&#194;\" >\n"
                + "<!ENTITY Atilde \"&#195;\" >\n"
                + "<!ENTITY Auml   \"&#196;\" >\n"
                + "<!ENTITY Aring  \"&#197;\" >\n"
                + "<!ENTITY AElig  \"&#198;\" >\n"
                + "<!ENTITY Ccedil \"&#199;\" >\n"
                + "<!ENTITY Egrave \"&#200;\" >\n"
                + "<!ENTITY Eacute \"&#201;\" >\n"
                + "<!ENTITY Ecirc  \"&#202;\" >\n"
                + "<!ENTITY Euml   \"&#203;\" >\n"
                + "<!ENTITY Igrave \"&#204;\" >\n"
                + "<!ENTITY Iacute \"&#205;\" >\n"
                + "<!ENTITY Icirc  \"&#206;\" >\n"
                + "<!ENTITY Iuml   \"&#207;\" >\n"
                + "<!ENTITY ETH    \"&#208;\" >\n"
                + "<!ENTITY Ntilde \"&#209;\" >\n"
                + "<!ENTITY Ograve \"&#210;\" >\n"
                + "<!ENTITY Oacute \"&#211;\" >\n"
                + "<!ENTITY Ocirc  \"&#212;\" >\n"
                + "<!ENTITY Otilde \"&#213;\" >\n"
                + "<!ENTITY Ouml   \"&#214;\" >\n"
                + "<!ENTITY times  \"&#215;\" >\n"
                + "<!ENTITY Oslash \"&#216;\" >\n"
                + "<!ENTITY Ugrave \"&#217;\" >\n"
                + "<!ENTITY Uacute \"&#218;\" >\n"
                + "<!ENTITY Ucirc  \"&#219;\" >\n"
                + "<!ENTITY Uuml   \"&#220;\" >\n"
                + "<!ENTITY Yacute \"&#221;\" >\n"
                + "<!ENTITY THORN  \"&#222;\" >\n"
                + "<!ENTITY szlig  \"&#223;\" >\n"
                + "<!ENTITY agrave \"&#224;\" >\n"
                + "<!ENTITY aacute \"&#225;\" >\n"
                + "<!ENTITY acirc  \"&#226;\" >\n"
                + "<!ENTITY atilde \"&#227;\" >\n"
                + "<!ENTITY auml   \"&#228;\" >\n"
                + "<!ENTITY aring  \"&#229;\" >\n"
                + "<!ENTITY aelig  \"&#230;\" >\n"
                + "<!ENTITY ccedil \"&#231;\" >\n"
                + "<!ENTITY egrave \"&#232;\" >\n"
                + "<!ENTITY eacute \"&#233;\" >\n"
                + "<!ENTITY ecirc  \"&#234;\" >\n"
                + "<!ENTITY euml   \"&#235;\" >\n"
                + "<!ENTITY igrave \"&#236;\" >\n"
                + "<!ENTITY iacute \"&#237;\" >\n"
                + "<!ENTITY icirc  \"&#238;\" >\n"
                + "<!ENTITY iuml   \"&#239;\" >\n"
                + "<!ENTITY eth    \"&#240;\" >\n"
                + "<!ENTITY ntilde \"&#241;\" >\n"
                + "<!ENTITY ograve \"&#242;\" >\n"
                + "<!ENTITY oacute \"&#243;\" >\n"
                + "<!ENTITY ocirc  \"&#244;\" >\n"
                + "<!ENTITY otilde \"&#245;\" >\n"
                + "<!ENTITY ouml   \"&#246;\" >\n"
                + "<!ENTITY divide \"&#247;\" >\n"
                + "<!ENTITY oslash \"&#248;\" >\n"
                + "<!ENTITY ugrave \"&#249;\" >\n"
                + "<!ENTITY uacute \"&#250;\" >\n"
                + "<!ENTITY ucirc  \"&#251;\" >\n"
                + "<!ENTITY uuml   \"&#252;\" >\n"
                + "<!ENTITY yacute \"&#253;\" >\n"
                + "<!ENTITY thorn  \"&#254;\" >\n"
                + "<!ENTITY yuml   \"&#255;\" >\n"
                + "]>\n";
    }

    public static NodeList getValueElementsFromXML(Document document) {
        try {
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList elements = (NodeList) xpath.evaluate("//VALUE", document.getDocumentElement(), XPathConstants.NODESET);
            return elements;
        } catch (XPathExpressionException ex) {
            return document.getElementsByTagName("nodelist");
        }
    }
}
